package com.glp.work.flow.config.filter;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import com.glp.work.flow.constant.Keys;

import lombok.extern.slf4j.Slf4j;

/** 
*
* @author  pengjia 
* @version 2020年8月24日 下午2:58:52 
*/
@Configuration
@Slf4j
public class WebTraceFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String traceId = request.getHeader(Keys.LOG_TRACE_ID);
        if(StringUtils.isEmpty(traceId)){
        	traceId = UUID.randomUUID().toString();
        }
        MDC.put(Keys.LOG_TRACE_ID, traceId);
        filterChain.doFilter(request, response);
        log.debug("清除相关MDC的内容："+Keys.LOG_TRACE_ID);
        MDC.remove(Keys.LOG_TRACE_ID);
	}

}
